<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>服务卡片 | East'blog</title><meta name="author" content="侯玉东"><meta name="copyright" content="侯玉东"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="服务卡片">
<meta property="og:type" content="article">
<meta property="og:title" content="服务卡片">
<meta property="og:url" content="https://houyudong1989.gitee.io/blog/2023/11/24/harmonyOS/%E6%9C%8D%E5%8A%A1%E5%8D%A1%E7%89%87/index.html">
<meta property="og:site_name" content="East&#39;blog">
<meta property="og:description" content="服务卡片">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg">
<meta property="article:published_time" content="2023-11-23T16:00:00.000Z">
<meta property="article:modified_time" content="2023-11-28T09:39:20.246Z">
<meta property="article:author" content="侯玉东">
<meta property="article:tag" content="harmonyOS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://houyudong1989.gitee.io/blog/img/library1.jpeg"><link rel="shortcut icon" href="/blog/img/head.jpg"><link rel="canonical" href="https://houyudong1989.gitee.io/blog/2023/11/24/harmonyOS/%E6%9C%8D%E5%8A%A1%E5%8D%A1%E7%89%87/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '服务卡片',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-11-28 17:39:20'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (true) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/blog/img/head.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('/blog/img/library6.jpeg')"><nav id="nav"><span id="blog-info"><a href="/blog/" title="East'blog"><img class="site-icon" src="/blog/img/head.jpg"/><span class="site-name">East'blog</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友联</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">服务卡片</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-11-23T16:00:00.000Z" title="发表于 2023-11-24 00:00:00">2023-11-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-28T09:39:20.246Z" title="更新于 2023-11-28 17:39:20">2023-11-28</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/harmonyOS/">harmonyOS</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="服务卡片"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">服务卡片是一种界面展示形式，可以将应用的重要信息或操作前置到卡片，以达到服务直达、减少体验层级的目的。卡片常用于嵌入到其他应用（当前卡片使用方只支持系统应用，如桌面）中作为其界面显示的一部分，并支持拉起页面、发送消息等基础的交互功能。</span><br></pre></td></tr></table></figure>

<h1 id="服务卡片架构"><a href="#服务卡片架构" class="headerlink" title="服务卡片架构"></a>服务卡片架构</h1><p><img src="1701050606907.png" alt="1701050606907"></p>
<h2 id="卡片基本概念"><a href="#卡片基本概念" class="headerlink" title="卡片基本概念"></a>卡片基本概念</h2><blockquote>
<p>卡片使用方</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">卡片使用方通常是指卡片的宿主应用，控制卡片显示的位置。目前只支持桌面使用</span><br></pre></td></tr></table></figure>

<blockquote>
<p>卡片提供方</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">卡片提供方就是我们的应用，通过FormExtensionAbility来实现卡片的业务逻辑。</span><br></pre></td></tr></table></figure>

<blockquote>
<p>卡片使用步骤</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">* 在桌面环境下，长按应用图标。</span><br><span class="line">* 在弹出的操作菜单中点击《服务卡片》选项。</span><br><span class="line">* 选择要添加的卡片，点击《添加至桌面》，即可在桌面看到新添加的卡片。</span><br></pre></td></tr></table></figure>

<h1 id="卡片实现原理"><a href="#卡片实现原理" class="headerlink" title="卡片实现原理"></a>卡片实现原理</h1><p><img src="1701051064685.png" alt="1701051064685"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">* 卡片使用方：显示卡片内容的宿主应用，控制卡片在宿主中展示的位置，当前仅系统应用可以作为卡片使用方。</span><br><span class="line">* 卡片提供方：提供卡片显示内容的应用，控制卡片的显示内容、控件布局以及控件点击事件。</span><br><span class="line">* 卡片管理服务：用于管理系统中所添加卡片的常驻代理服务，提供formProvider接口能力，同时提供卡片对象的管理与使用以及卡片周期性刷新等能力。</span><br><span class="line">* 卡片渲染服务：用于管理卡片渲染实例，渲染实例与卡片使用方上的卡片组件一一绑定。卡片渲染服务运行卡片页面代码widgets.abc进行渲染，并将渲染后的数据发送至卡片使用方对应的卡片组件。</span><br></pre></td></tr></table></figure>

<p><img src="1701051302450.png" alt="1701051302450">*</p>
<blockquote>
<ul>
<li>一个应用可以有多个卡片。</li>
<li>每个卡片对应一个渲染实例，保证卡片出现问题不会影响应用使用。</li>
<li>同一个应用所有卡片运行在同一个虚拟机运行环境中，资源进行共享。</li>
<li>不同应用资源相互隔离。</li>
<li>相同应用提供方的卡片globalThis对象是同一个，不同应用提供方的卡片globalThis对象是不同的。</li>
</ul>
</blockquote>
<h1 id="卡片创建"><a href="#卡片创建" class="headerlink" title="卡片创建"></a>卡片创建</h1><h2 id="创建卡片"><a href="#创建卡片" class="headerlink" title="创建卡片"></a>创建卡片</h2><p><img src="1701051699871.png" alt="1701051699871"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在工程目录中选择右键-》New-》Service Widget，进行创建卡片</span><br></pre></td></tr></table></figure>

<h2 id="选择卡片模版"><a href="#选择卡片模版" class="headerlink" title="选择卡片模版"></a>选择卡片模版</h2><p><img src="1701051795566.png" alt="1701051795566"></p>
<p><img src="1701051815259.png" alt="1701051815259"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">根据业务要求，选择卡片模版，同时设置卡片开发语言类型为ArkTs，选择卡片支持的大小。最后单击《Finish》，即可创建卡片。</span><br></pre></td></tr></table></figure>

<h2 id="卡片结构"><a href="#卡片结构" class="headerlink" title="卡片结构"></a>卡片结构</h2><p><img src="1701052240871.png" alt="1701052240871"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">卡片创建完成以后，在项目目录中会新增卡片相关文件，包括：卡片生命周期管理文件EntryFormAbility、卡片的UI页面文件WidgetCard，卡片配置文件form_config.json，同时要在模块的module.json5中配置相关参数。</span><br></pre></td></tr></table></figure>

<h2 id="配置卡片信息"><a href="#配置卡片信息" class="headerlink" title="配置卡片信息"></a>配置卡片信息</h2><blockquote>
<p>module.json5 配置文件</p>
<p>在该文件中，需要在extensionAbilities标签下，配置FormExtensionAbility相关信息，metadata元信息标签，其中键名称为固定字符串“ohos.extension.form”，资源为卡片的具体配置信息的索引。</p>
</blockquote>
<p><img src="1701052716493.png" alt="1701052716493"></p>
<blockquote>
<p>form_config.json配置文件</p>
</blockquote>
<p><img src="1701052932275.png" alt="1701052932275"></p>
<blockquote>
<p>配置属性表</p>
</blockquote>
<table>
<thead>
<tr>
<th align="left">属性名称</th>
<th align="left">含义</th>
<th align="left">数据类型</th>
<th align="left">是否可缺省</th>
</tr>
</thead>
<tbody><tr>
<td align="left">name</td>
<td align="left">表示卡片的类名，字符串最大长度为127字节。</td>
<td align="left">字符串</td>
<td align="left">否</td>
</tr>
<tr>
<td align="left">description</td>
<td align="left">表示卡片的描述。取值可以是描述性内容，也可以是对描述性内容的资源索引，以支持多语言。字符串最大长度为255字节。该属性将显示在<a target="_blank" rel="noopener" href="https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/service-widget-overview-0000001536226057-V3#ZH-CN_TOPIC_0000001523648622__fig149024251313">卡片预览</a>界面上，以便用户识别不同卡片。</td>
<td align="left">字符串</td>
<td align="left">可缺省，缺省为空。</td>
</tr>
<tr>
<td align="left">src</td>
<td align="left">表示卡片对应的UI代码的完整路径。当为ArkTS卡片时，完整路径需要包含卡片文件的后缀，如：”.&#x2F;ets&#x2F;widget&#x2F;pages&#x2F;WidgetCard.ets”。当为JS卡片时，完整路径无需包含卡片文件的后缀，如：”.&#x2F;js&#x2F;widget&#x2F;pages&#x2F;WidgetCard”</td>
<td align="left">字符串</td>
<td align="left">否</td>
</tr>
<tr>
<td align="left">uiSyntax</td>
<td align="left">表示该卡片的类型，当前支持如下两种类型：- arkts：当前卡片为ArkTS卡片。- hml：当前卡片为JS卡片。</td>
<td align="left">字符串</td>
<td align="left">可缺省，缺省值为hml</td>
</tr>
<tr>
<td align="left">window</td>
<td align="left">用于定义与显示窗口相关的配置。</td>
<td align="left">对象</td>
<td align="left">可缺省</td>
</tr>
<tr>
<td align="left">isDefault</td>
<td align="left">表示该卡片是否为默认卡片，每个UIAbility有且只有一个默认卡片。- true：默认卡片。- false：非默认卡片。</td>
<td align="left">布尔值</td>
<td align="left">否</td>
</tr>
<tr>
<td align="left">colorMode</td>
<td align="left">表示卡片的主题样式，取值范围如下：- auto：自适应。- dark：深色主题。- light：浅色主题。</td>
<td align="left">字符串</td>
<td align="left">可缺省，缺省值为“auto”。</td>
</tr>
<tr>
<td align="left">supportDimensions</td>
<td align="left">表示卡片支持的外观规格，取值范围：- 1 * 2：表示1行2列的二宫格。- 2 * 2：表示2行2列的四宫格。- 2 * 4：表示2行4列的八宫格。- 4 * 4：表示4行4列的十六宫格。</td>
<td align="left">字符串数组</td>
<td align="left">否</td>
</tr>
<tr>
<td align="left">defaultDimension</td>
<td align="left">表示卡片的默认外观规格，取值必须在该卡片supportDimensions配置的列表中。</td>
<td align="left">字符串</td>
<td align="left">否</td>
</tr>
<tr>
<td align="left">updateEnabled</td>
<td align="left">表示卡片是否支持周期性刷新（包含定时刷新和定点刷新），取值范围：- true：表示支持周期性刷新，可以在定时刷新（updateDuration）和定点刷新（scheduledUpdateTime）两种方式任选其一，当两者同时配置时，定时刷新优先生效。- false：表示不支持周期性刷新。</td>
<td align="left">布尔类型</td>
<td align="left">否</td>
</tr>
<tr>
<td align="left">scheduledUpdateTime</td>
<td align="left">表示卡片的定点刷新的时刻，采用24小时制，精确到分钟。&gt;<strong>说明：</strong>&gt; updateDuration参数优先级高于scheduledUpdateTime，两者同时配置时，以updateDuration配置的刷新时间为准。</td>
<td align="left">字符串</td>
<td align="left">可缺省，缺省时不进行定点刷新。</td>
</tr>
<tr>
<td align="left">updateDuration</td>
<td align="left">表示卡片定时刷新的更新周期，单位为30分钟，取值为自然数。当取值为0时，表示该参数不生效。当取值为正整数N时，表示刷新周期为30*N分钟。&gt;<strong>说明：</strong>&gt; updateDuration参数优先级高于scheduledUpdateTime，两者同时配置时，以updateDuration配置的刷新时间为准。</td>
<td align="left">数值</td>
<td align="left">可缺省，缺省值为“0”。</td>
</tr>
<tr>
<td align="left">formConfigAbility</td>
<td align="left">表示卡片的配置跳转链接，采用URI格式。</td>
<td align="left">字符串</td>
<td align="left">可缺省，缺省值为空。</td>
</tr>
<tr>
<td align="left">formVisibleNotify</td>
<td align="left">标识是否允许卡片使用卡片可见性通知。</td>
<td align="left">字符串</td>
<td align="left">可缺省，缺省值为空。</td>
</tr>
<tr>
<td align="left">metadata</td>
<td align="left">表示卡片的自定义信息，包含customizeData数组标签。</td>
<td align="left">对象</td>
<td align="left">可缺省，缺省值为空。</td>
</tr>
</tbody></table>
<h2 id="卡片生命周期"><a href="#卡片生命周期" class="headerlink" title="卡片生命周期"></a>卡片生命周期</h2><blockquote>
<p>onAddForm(want)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">使用方创建卡片时触发，提供方需要返回卡片数据绑定类</span><br></pre></td></tr></table></figure>

<blockquote>
<p>onCastToNormalForm(formId)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">使用方将临时卡片转换为常态卡片触发，提供方需要做相应的处理</span><br></pre></td></tr></table></figure>

<blockquote>
<p>onUpdateForm(formId)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">若卡片支持定时更新/定点更新/卡片使用方主动请求更新功能，则提供方需要重写该方法以支持数据更新</span><br></pre></td></tr></table></figure>

<blockquote>
<p>onChangeFormVisibility(newStatus)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">需要配置formVisibleNotify为true，且为系统应用才会回调</span><br></pre></td></tr></table></figure>

<blockquote>
<p>onFormEvent(formId, message)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">若卡片支持触发事件，则需要重写该方法并实现对事件的触发</span><br></pre></td></tr></table></figure>

<blockquote>
<p>onRemoveForm(formId)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">当对应的卡片删除时触发的回调，入参是被删除的卡片ID</span><br></pre></td></tr></table></figure>

<blockquote>
<p>onConfigurationUpdate(config)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">当系统配置信息置更新时触发的回调</span><br></pre></td></tr></table></figure>

<blockquote>
<p>onAcquireFormState(want)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">卡片提供方接收查询卡片状态通知接口，默认返回卡片初始状态</span><br></pre></td></tr></table></figure>

<h1 id="卡片跳转事件-postCardAction"><a href="#卡片跳转事件-postCardAction" class="headerlink" title="卡片跳转事件 postCardAction"></a>卡片跳转事件 postCardAction</h1><h2 id="router跳转页面"><a href="#router跳转页面" class="headerlink" title="router跳转页面"></a>router跳转页面</h2><blockquote>
<p>通过router可以快速拉起应用指定的Uiability，实现一键直达的效果。</p>
</blockquote>
<blockquote>
<p>WidgetCard页面</p>
<p>在WidgetCard页面定义两个点击事件，当点击事件触发以后，会执行EntryAbility函数，根据EntryAbility是否是第一次启动会执行不同的生命周期函数onCreate或者onNewWant</p>
</blockquote>
<p><img src="1701066006000.png" alt="1701066006000"></p>
<blockquote>
<p>EntryAbility类</p>
<p>当点击事件触发以后，在onCreate或onNewWant中获取want参数，在去跳转到指定页面，如果当前EntryAbility已有实例，那么进入onNewWant函数，不会执行onCreate函数，需要手动调用onWindowStageCreate触发页面更新。</p>
</blockquote>
<p><img src="1701066157294.png" alt="1701066157294"></p>
<h2 id="call-拉起后台"><a href="#call-拉起后台" class="headerlink" title="call 拉起后台"></a>call 拉起后台</h2><blockquote>
<p>要求UIability启动模式必须为单例模式，同时需具备后台运行权限（ohos.permission.KEEP_BACKGROUND_RUNNING），同时传递参数时必须传入method，且类型需要为string类型，用于触发UIAbility中对应的方法。</p>
</blockquote>
<blockquote>
<p>目前还有个疑问？获取不到传递的自定义参数</p>
</blockquote>
<p><img src="1701074265917.png" alt="1701074265917"></p>
<p><img src="1701074291974.png" alt="1701074291974"></p>
<h2 id="message-刷新卡片内容"><a href="#message-刷新卡片内容" class="headerlink" title="message 刷新卡片内容"></a>message 刷新卡片内容</h2><blockquote>
<p>刷新卡片内容分为两种形式，一种是手动点击刷新，一种是定时刷新，定时刷新又分为指定时间刷新和固定周期刷新，固定周期最低30分钟一次。</p>
</blockquote>
<blockquote>
<p>下图为EntryFormAbility逻辑，每次调用更新会执行onFormEvent方法，将title和detail两个字段存储到LocalStorage，WidgetCard会将两个字段更新后刷新显示。</p>
<p>注意：下图中num的生命周期只有5秒，刷新停止后五秒，该函数失效，再次调用num又重新开始。</p>
</blockquote>
<p><img src="1701064645124.png" alt="1701064645124"></p>
<blockquote>
<p>下图为UI页面，点击事件发生后，会调用EntryFormAbility函数进行逻辑处理，并同步Ui刷新数据，params中数据会传递给onFormEvent中的message字段。</p>
</blockquote>
<p><img src="1701064662588.png" alt="1701064662588"></p>
<p><a target="_blank" rel="noopener" href="https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/service-widget-overview-0000001536226057-V3">官方链接</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog">侯玉东</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://houyudong1989.gitee.io/blog/2023/11/24/harmonyOS/%E6%9C%8D%E5%8A%A1%E5%8D%A1%E7%89%87/">https://houyudong1989.gitee.io/blog/2023/11/24/harmonyOS/%E6%9C%8D%E5%8A%A1%E5%8D%A1%E7%89%87/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://houyudong1989.gitee.io/blog" target="_blank">East'blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/harmonyOS/">harmonyOS</a></div><div class="post_share"><div class="social-share" data-image="/blog/img/library1.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2023/11/24/harmonyOS/%E5%83%8F%E7%B4%A0%E5%8D%95%E4%BD%8D/" title="像素单位"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">像素单位</div></div></a></div><div class="next-post pull-right"><a href="/blog/2023/11/22/%E4%BF%A1%E6%81%AF%E9%A1%B9%E7%9B%AE%E7%B3%BB%E7%BB%9F%E7%AE%A1%E7%90%86/15%E3%80%81%E9%A3%8E%E9%99%A9%E7%AE%A1%E7%90%86/" title="风险管理"><img class="cover" src="/blog/img/library1.jpeg" onerror="onerror=null;src='/blog/img/friend_404.gif'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">风险管理</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/blog/2024/01/20/harmonyOS/ArkUI-X/" title="ArkUI-X"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-01-20</div><div class="title">ArkUI-X</div></div></a></div><div><a href="/blog/2023/12/15/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%8A%98%E7%BA%BF%E5%9B%BE/" title="canvas绘制折线图"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-12-15</div><div class="title">canvas绘制折线图</div></div></a></div><div><a href="/blog/2023/12/02/harmonyOS/Canvas%E7%BB%98%E5%88%B6%E6%9F%B1%E7%8A%B6%E5%9B%BE/" title="canvas绘制柱状图"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-12-02</div><div class="title">canvas绘制柱状图</div></div></a></div><div><a href="/blog/2024/03/25/harmonyOS/commonevent%E3%80%81eventhub%E3%80%81emitter%E5%92%8Cworker%E4%BD%BF%E7%94%A8/" title="commonevent、eventhub、emitter和worker使用"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-03-25</div><div class="title">commonevent、eventhub、emitter和worker使用</div></div></a></div><div><a href="/blog/2023/11/05/harmonyOS/hdc/" title="hdc 使用帮助"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-11-05</div><div class="title">hdc 使用帮助</div></div></a></div><div><a href="/blog/2023/12/26/harmonyOS/router%E8%B7%AF%E7%94%B1/" title="router路由跳转"><img class="cover" src="/blog/img/library1.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-12-26</div><div class="title">router路由跳转</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/blog/img/head.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">侯玉东</div><div class="author-info__description">生命不息，奋斗不止</div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">70</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/houyudong1989"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/houyudong1989" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:420098321@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到我的博客！！！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%A6%82%E8%BF%B0"><span class="toc-number">1.</span> <span class="toc-text">概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%9C%8D%E5%8A%A1%E5%8D%A1%E7%89%87%E6%9E%B6%E6%9E%84"><span class="toc-number">2.</span> <span class="toc-text">服务卡片架构</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%A1%E7%89%87%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">2.1.</span> <span class="toc-text">卡片基本概念</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%A1%E7%89%87%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86"><span class="toc-number">3.</span> <span class="toc-text">卡片实现原理</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%A1%E7%89%87%E5%88%9B%E5%BB%BA"><span class="toc-number">4.</span> <span class="toc-text">卡片创建</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%8D%A1%E7%89%87"><span class="toc-number">4.1.</span> <span class="toc-text">创建卡片</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E5%8D%A1%E7%89%87%E6%A8%A1%E7%89%88"><span class="toc-number">4.2.</span> <span class="toc-text">选择卡片模版</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%A1%E7%89%87%E7%BB%93%E6%9E%84"><span class="toc-number">4.3.</span> <span class="toc-text">卡片结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E5%8D%A1%E7%89%87%E4%BF%A1%E6%81%AF"><span class="toc-number">4.4.</span> <span class="toc-text">配置卡片信息</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%A1%E7%89%87%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">4.5.</span> <span class="toc-text">卡片生命周期</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%A1%E7%89%87%E8%B7%B3%E8%BD%AC%E4%BA%8B%E4%BB%B6-postCardAction"><span class="toc-number">5.</span> <span class="toc-text">卡片跳转事件 postCardAction</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#router%E8%B7%B3%E8%BD%AC%E9%A1%B5%E9%9D%A2"><span class="toc-number">5.1.</span> <span class="toc-text">router跳转页面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#call-%E6%8B%89%E8%B5%B7%E5%90%8E%E5%8F%B0"><span class="toc-number">5.2.</span> <span class="toc-text">call 拉起后台</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#message-%E5%88%B7%E6%96%B0%E5%8D%A1%E7%89%87%E5%86%85%E5%AE%B9"><span class="toc-number">5.3.</span> <span class="toc-text">message 刷新卡片内容</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="字符和字符串"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/5%E3%80%81%E5%AD%97%E7%AC%A6%E5%92%8C%E5%AD%97%E7%AC%A6%E4%B8%B2/" title="字符和字符串">字符和字符串</a><time datetime="2024-05-06T11:15:27.280Z" title="发表于 2024-05-06 19:15:27">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="指针和函数指针"/></a><div class="content"><a class="title" href="/blog/2024/05/06/c++/4%E3%80%81%E6%8C%87%E9%92%88%E5%92%8C%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" title="指针和函数指针">指针和函数指针</a><time datetime="2024-05-06T11:14:29.047Z" title="发表于 2024-05-06 19:14:29">2024-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="m3u8格式"/></a><div class="content"><a class="title" href="/blog/2024/05/05/python/7%E3%80%81m3u8%E8%A7%86%E9%A2%91%E6%A0%BC%E5%BC%8F/" title="m3u8格式">m3u8格式</a><time datetime="2024-05-05T01:43:26.865Z" title="发表于 2024-05-05 09:43:26">2024-05-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="charles 抓包"/></a><div class="content"><a class="title" href="/blog/2024/04/30/android/charles%E5%AE%89%E5%8D%93%E6%89%8B%E6%9C%BA%E6%8A%93%E5%8C%85/" title="charles 抓包">charles 抓包</a><time datetime="2024-04-30T06:49:29.279Z" title="发表于 2024-04-30 14:49:29">2024-04-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright"><img src="/blog/img/library1.jpeg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="playwright"/></a><div class="content"><a class="title" href="/blog/2024/04/26/python/13%E3%80%81playwright/" title="playwright">playwright</a><time datetime="2024-04-26T07:30:36.069Z" title="发表于 2024-04-26 15:30:36">2024-04-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By 侯玉东</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="8873950678" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true"> </div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="false"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener
  btf.removeGlobalFnEvent('pjax')
  btf.removeGlobalFnEvent('themeChange')

  document.getElementById('rightside').classList.remove('rightside-show')
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', e => {
  if (e.request.status === 404) {
    pjax.loadUrl('/blog/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>